/*
 * @Author: huangjian mentu@ttigd.com
 * @Date: 2024-07-04 14:19:51
 * @LastEditors: huangjian mentu@ttigd.com
 * @LastEditTime: 2024-07-04 15:38:56
 * @FilePath: \cocosQRCodeProject\assets\scripts\QRTest.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { _decorator, Color, Component, EditBox, Graphics, Node, UITransform } from 'cc';
import { QRCode } from './QRcode/QRCode';
// import { QRCode } from "../../../platform_sdk/shunwang/QRcode/QRCode";
const { ccclass, property } = _decorator;

@ccclass('QRTest')
export class QRTest extends Component {
    @property(Node)
    ctxNode: Node = null;

    @property(EditBox)
    editBox: EditBox = null;
    start() {

    }

    update(deltaTime: number) {
        
    }

        /**
     * 绘制二维码
     * @param ctxNode 显示的节点
     * @param url 文本内容
     * @param Wcolor 白块颜色
     * @param Hcolor 黑块颜色
     */
        QRCreate(url, Wcolor = Color.WHITE, Hcolor = Color.BLACK) {
            let editUrl = this.editBox.string;
            console.log("editUrl",editUrl);
            url = "iVBORw0KGgoAAAANSUhEUgAAAIwAAACMAQAAAACGu3yTAAACrElEQVR42uXWPZKgKBQAYEjgCpLI1TSRK0ACkuAVIIGrSQLphpDAMNW9s1Xd/fYCQ/iVWg94P6L5df2D/gpqaNciN62F0/TGAPUZnGZzkE1fInWIbPKmvpI2ZR/PQAppFpYe20z9H4rKqXwjam8B0wrC2Jkc0jT9F9cXaoiFP+vfPX6jtXLRBw+v5vnPeX2lhm0OT0SEWb82DtAgKjn1ankdZ1MPQDOig0a908aL5gmgPmeIwiYnNX8lhigHjz26JL8v7AxA08uzuutch8iCpwA15lXjcz63FtEEiPT5EnoffI7tMB2gHhzvvYlcWLcvhSi/ar0iVc/xQhA1zdLg46J5Vq8VQDPUYRySbAWdBwNorOwo5pbbevUlAiIiopq9sJm9+TicH6hJ1l88+JMjT8+E6Mxepdw2LemrDUC9oLOY2UR4xpkTQLNm6y9V286KcRigJvEtokQ0R9pwBWiwHvVKJlsbzR2iprxZPYEFG7IND0BzPb12w/21Cr6DdCNy4YbIwatHCqB1rYcmeLZL82ESQL1sUpLroumVh6gANfbuRNVnXXGhBQPU3U40rp4gTXjIADWi7v1SA10HbWYCNJbw7i78tO2SFCJcNlrzLTXaiOQA9Ya0xkWlm4hxPgCtZMo1ssef7y5mh0g5OohE3PPHfpTCDzTMTNbaggse4jPvv1OvDuF4sLiJGpEBqKmaV2ev810nvTOIdtofJwnhtu3iAaivT/cbZycPTSMHaRM2nvdKcrGCBqjhd9uF/Z1TrHx2k+80Zwr1casBJY+QAKlnT2tyLBTWKEBr6Di0meCwozZ0gPpMZc1dvjKT9R4gWsUu3pUj8mD+I4ifaTW9NQac8qsgYGKrWa1LGXQNYoimX43fTn+t8fpZHd/p94Y2PHYR7uNMFaC/8mfrF3Vrra3RXHAjAAAAAElFTkSuQmCC"
            //根据QRCode来生成二维码数据
            var qrcode = new QRCode(-1, 2);
            qrcode.addData(url);
            qrcode.make();
            let ctx = this.ctxNode.getComponent(Graphics);
            ctx.fillColor = Color.BLACK;
            let roll = Math.floor(qrcode.getModuleCount());
            let width = this.ctxNode.getComponent(UITransform).contentSize.width;
            let height = this.ctxNode.getComponent(UITransform).contentSize.height;
            //块宽高
            var tileW = this.ctxNode.getComponent(UITransform).contentSize.width / roll;
            var tileH = this.ctxNode.getComponent(UITransform).contentSize.height / roll;
            ctx.clear();
            for (var row = 0; row < qrcode.getModuleCount(); row++) {
                for (var col = 0; col < qrcode.getModuleCount(); col++) {
                    var w = Math.ceil((col + 1) * tileW) - Math.floor(col * tileW);
                    var h = Math.ceil((row + 1) * tileW) - Math.floor(row * tileW);
                    let posx =
                    Math.round(col * tileW) -
                    this.ctxNode.getComponent(UITransform).contentSize.width / 2;
                    let posy =
                    Math.round(row * tileH) -
                    this.ctxNode.getComponent(UITransform).contentSize.height / 2;
    
                    ctx.rect(posx, posy, w, h);
                    //颜色
                    if (qrcode.isDark(row, col)) {
                    ctx.fillColor = Hcolor;
                    } else {
                    ctx.fillColor = Wcolor;
                    }
                    ctx.fill();
                }
            }
        }
}


